﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Services;

namespace app.Api
{
    /// <summary>
    ///MessageHelper 的摘要说明
    /// </summary>
    public class MessageHelper
    {
        public MessageHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }


        public static Boolean CheckPhoneNumber(string txtPhoneNum)
        {
            string regex = "^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$";
            //string regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
            return System.Text.RegularExpressions.Regex.IsMatch(txtPhoneNum, regex);
        }
        /// <summary>
        /// 发送手机验证码
        /// </summary>
        /// <param name="txtPhoneNum"></param>
        /// <param name="checkCode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string GetCodeByPhone(string txtPhoneNum, string msg)
        {
            WebService.Service1 ws = new WebService.Service1();
            string flag = "";
            string name = app.Models.FreeValue.GetValue("phone.username");
            string pass = app.Models.FreeValue.GetValue("phone.password");
            string result = ws.SendMessages(name, pass, txtPhoneNum, msg, "");
            string reason = "";
            if (result == "-01")
            {
                reason = "当前账号余额不足！";
                flag = "false";
            }
            else if (result == "-02")
            {
                reason = "当前用户ID错误！";
                flag = "false";
            }
            else if (result == "-03")
            {
                reason = "当前密码错误！";
                flag = "false";
            }
            else if (result == "-04")
            {
                reason = "参数不够或参数内容的类型错误！";
                flag = "false";
            }
            else if (result == "-05")
            {
                reason = "手机号码格式不对！";
                flag = "phone_false";
            }
            else if (result == "-06")
            {
                reason = "短信内容编码不对！";
                flag = "false";
            }
            else if (result == "-07")
            {
                reason = "短信内容含有敏感字符！";
                flag = "false";
            }
            else if (result == "-08")
            {
                reason = "无接收数据！";
                flag = "false";
            }
            else if (result == "-09")
            {
                reason = "系统维护中..";
                flag = "false";
            }
            else if (result == "-10")
            {
                reason = "手机号码数量超长！";
                flag = "false";
            }
            else if (result == "-11")
            {
                reason = "短信内容超长（70个字符）！";
                flag = "false";
            }
            else if (result == "-12")
            {
                reason = "其它错误！";
                flag = "false";
            }
            else if (result == "-13")
            {
                reason = "文件传输错误！";
                flag = "false";
            }
            else
            {
                flag = "success";
            }
            return flag;
        }
        /// <summary>
        /// 得到六位数字验证码
        /// </summary>
        /// <returns></returns>
        public static string GetCode()
        {
            int number;
            char code;
            string checkCode = string.Empty;
            Random random = new Random();
            for (int i = 0; i < 6; i++)
            {
                number = random.Next();
                code = (char)('0' + (char)(number % 10));
                checkCode += code.ToString();
            }
            return checkCode;
        }
        //string result = ws.SendMessages("hong_du", "wtruanjian", txtPhoneNum, "尊敬的用户，您的注册验证码是" + checkCode + "。", "");
        //    string expresstion = new Hongdu.BLL.t_rule().GetExpresstion("QT003");
        //    if (!string.IsNullOrEmpty(expresstion))
        //    {
        //        string[] arr = expresstion.Split(';');
        //        string messageUser = arr[0];
        //        string messagePsw = arr[1];
        //        string result = ws.SendMessages(messageUser, messagePsw, txtPhoneNum, msg, "");

        //    }




        ///// <summary>
        ///// 管理员向指定会员群发短信
        ///// </summary>
        ///// <param name="txtPhoneNum">手机号，以","号分隔</param>
        ///// <param name="msg">群发内容，不能超过70字符</param>
        ///// <returns>true/false</returns>
        //public static bool SendMessages(string txtPhoneNum,string msg)
        //{
        //    WebService.Service1 ws = new WebService.Service1();
        //    string expresstion = new Hongdu.BLL.t_rule().GetExpresstion("QT003");
        //    if (!string.IsNullOrEmpty(expresstion))
        //    {
        //        string[] arr = expresstion.Split(';');
        //        string messageUser = arr[0];
        //        string messagePsw = arr[1];
        //        string result = ws.SendMessages(messageUser, messagePsw, txtPhoneNum, msg, "");
        //        if (result == "-01")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：当前账号余额不足!");
        //            return false;
        //        }
        //        else if (result == "-02")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：当前用户ID错误！");
        //            return false;
        //        }
        //        else if (result == "-03")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：密码错误");
        //            return false;
        //        }
        //        else if (result == "-04")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：参数不够或参数内容的类型错误");
        //            return false;
        //        }
        //        else if (result == "-05")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：手机号码格式不对");
        //            return false;
        //        }
        //        else if (result == "-06")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：短信内容编码不对");
        //            return false;
        //        }
        //        else if (result == "-07")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：短信内容含有敏感字符");
        //            return false;
        //        }
        //        else if (result == "-08")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：无接收数据");
        //            return false;
        //        }
        //        else if (result == "-09")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：系统维护中");
        //            return false;
        //        }
        //        else if (result == "-10")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：手机号码数量超长");
        //            return false;
        //        }
        //        else if (result == "-11")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：短信内容超长（70个字符）！");
        //            return false;
        //        }
        //        else if (result == "-12")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：其它错误");
        //            return false;
        //        }
        //        else if (result == "-13")
        //        {
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", "管理员群发短信发送失败，原因：文件传输错误!");
        //            return false;
        //        }
        //        else
        //        {
        //            int num = txtPhoneNum.Split(',').Length;
        //            Hongdu.DAL.PE_LogDAL.AdminInsert("群发短信", string.Format("管理员群发短信成功，共发送{0}条", num));
        //            return true;
        //        }
        //    }
        //    else {
        //        return false;
        //    }
        //}





        ////Shengjie add 2015/1/17
        ///// <summary>
        ///// 通过输入更新关注度的类型计算出需要增加的关注度，type包括"views"、"match"、"inquiry"三种参数
        ///// </summary>
        ///// <param name="type"></param>
        ///// <returns></returns>
        //public static double GetNewAttentionRate(string type) {
        //    //通过规则code得到关注度表达式内容：$Views$*0.2+$Match$*0.6+$Query$*0.2     
        //    string expresstion = new Hongdu.BLL.t_rule().GetExpresstion("QT001");
        //    if (type == "views")
        //    {
        //        expresstion = expresstion.Replace("$Views$", "1").Replace("$Match$", "0").Replace("$Query$", "0");
        //    }
        //    else if (type == "match") {
        //        expresstion = expresstion.Replace("$Views$", "0").Replace("$Match$", "1").Replace("$Query$", "0");
        //    }
        //    else if (type == "inquiry")
        //    {
        //        expresstion = expresstion.Replace("$Views$", "0").Replace("$Match$", "0").Replace("$Query$", "1");
        //    }
        //    MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
        //    sc.Language = "javascript";
        //    object o = sc.Eval(expresstion);
        //    double result = Convert.ToDouble(sc.Eval(expresstion));
        //    return result;
        //}

        ///// <summary>
        ///// 判断会员是否可以看联系人的信息，code表示当前登录的会员，pulish_rcode表示该供货/求购信息的发布人的编号
        ///// </summary>
        ///// <param name="code"></param>
        ///// <param name="pulish_rcode"></param>
        ///// <returns></returns>
        //public static bool IsView(string code, string pulish_rcode)
        //{
        //    Hongdu.Model.t_register userModel = new Hongdu.BLL.t_register().GetByCode(code);
        //    if (userModel.r_nickname == "1")
        //    {
        //        //如果该会员有权限查看联系人的信息
        //        return true;
        //    }
        //    else
        //    {
        //        //如果该会员没有权限查看联系人的信息
        //        Hongdu.BLL.t_level_authority levelBLL = new Hongdu.BLL.t_level_authority();
        //        //根据会员星级查看，该会员的此时的会员星级可不可以查看联系人的信息
        //        if (levelBLL.IsEnable("see", code))
        //        {
        //            //如果该会员的会员星级可以查看联系人的信息
        //            return true;
        //        }
        //        else
        //        {
        //            //如果该会员的会员星级不可以查看联系人的信息
        //            //判断是否发送了订单意向书
        //            Hongdu.BLL.t_order_indent indentBLL = new Hongdu.BLL.t_order_indent();
        //            if (indentBLL.IsSendIndent(code, pulish_rcode))
        //            {
        //                //如果发送了订单意向书
        //                return true;
        //            }
        //            else
        //            {
        //                //如果没有发送订单意向书
        //                return false;
        //            }
        //        }
        //    }
        //}


        public static string NoHTML(string Htmlstring)  //替换HTML标记
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);

            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<img[^>]*>;", "", RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
            return Htmlstring;
        }


    }
}
